var app = getApp();
var ajax = require('../../utils/ajax.js');
var util = require('../../utils/util.js');
var MPage = require('../MPage.js');
var common = require('../../utils/common.js');
var isPageEnd = false;
var nowPage = 1;
var timer = null;

Page(common.extendPage(MPage, {
  /**
   * 页面的初始数据
   */
  data: {
    dataLoaded: false,
    nowPage: 1,
    userName: "",
    myHidden: false,
    inputValue: "",
    gpsFail: false,
    nearbyStoresList: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function() {
    var _self = this;
    var nowPage = _self.data.nowPage;
    // 生命周期函数--监听页面加载
    this.setData({
      loading_hidden: false,
      nowPage: 1,
      // userInfo: app.getUserData(),
    })

    var {
      lat,
      lng
    } = app.getLocation();
    if (!lat || !lng) {
      wx.getLocation({
        success: function(result) {
          var lat = result.latitude;
          var lng = result.longitude;
          app.setLocation(lat, lng);
          _self.getStoreList(nowPage);
        },
        fail: function(result) {
          _self.setData({
            gpsFail: true,
            loadingState: true
          })
        }
      });
    } else {
      _self.getStoreList(nowPage);
    }
  },
  onShareAppMessage: function() {
    return {
      title: this.data.shop.shareTitle,
      path: "pages/index/index"
    }
  },
  onPullDownRefresh: function() {
    this.setData({
      nowPage: 1,
      nearbyStoresList: [],
    })
    var nowPage = this.data.nowPage;
    this.getStoreList(nowPage);
    isPageEnd = false;
  },
  onReachBottom: function() {
    if (isPageEnd) {
      return;
    }
    this.setData({
      nowPage: this.data.nowPage + 1
    })
    var nowPage = this.data.nowPage;
    this.getStoreList(nowPage);

  },
  /**
   * 重新授权共享地址
   */
  _getGPS: function(evt) {
    var _self = this;
    var nowPage = _self.data.nowPage;
    var formId = evt.detail.formId;
    _self.setData({
      dataLoaded: false,
      gpsFail: false
    })
    wx.openSetting({
      success: function() {
        wx.getLocation({
          success: function(result) {
            var lat = result.latitude;
            var lng = result.longitude;
            app.setLocation(lat, lng);
            _self.getStoreList(nowPage);
          },
          fail: function(result) {
            _self.setData({
              gpsFail: true,
              dataLoaded: true
            })
          }
        });
      }
    })
    common.submitForm(formId);
  },
  /**
   * 跳转至地图
   */
  _toMap: function(evt) {
    var shop = evt.currentTarget.dataset;
    var formId = evt.detail.formId;

    ajax.G('/openapi/CAddress/getLocation', {
      addr: shop.address
    }, function(data) {
      var addressInfo = JSON.parse(data.result)['result'];

      wx.openLocation({
        latitude: addressInfo.location.lat,
        longitude: addressInfo.location.lng,
        name: shop.name,
        address: shop.address,
      })
    });
    common.submitForm(formId);
  },
  /**
   * 获取商铺列表
   */
  getStoreList: function(nowPage) {
    var _self = this;
    var {
      lat,
      lng
    } = app.getLocation();
    ajax.G('/openapi/CHome/nearbyStoresList', {
      page: nowPage,
      lng,
      lat,
    }, function(data) {

      var o = _self.data.nearbyStoresList;
      var d = data.result['nearbyStoresList'] || [];
      d.forEach(function(stores) {
        var range = stores.juli;

        if (range <= 100) {
          range = '100米以内';
        } else if (range <= 1000) {
          range = range + ' m';
        } else {
          range = (parseFloat(range) / 1000).toFixed(2) + ' km';
        }
        stores.juli = range;

      });
      if (!d || d.length < 10) {
        isPageEnd = true;
      }
      if (d && d.length > 0) {
        o.push(d);
      }

      _self.setData({
        nearbyStoresList: o,
        dataLoaded: true,
      });
    });
  },

}))